package com.dormitory.dormitory_management.service.impl;

import com.dormitory.dormitory_management.dao.AddUser;
import com.dormitory.dormitory_management.dao.DormitoryDao;
import com.dormitory.dormitory_management.dao.SelectId;
import com.dormitory.dormitory_management.domain.StudentDorm;
import com.dormitory.dormitory_management.domain.UserInfo;
import com.dormitory.dormitory_management.service.DormitoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class DormitoryServiceImpl implements DormitoryService {
    @Autowired
    private DormitoryDao dormitoryDao;
    @Autowired
    private AddUser adduser;
    @Override
    public List<StudentDorm> findAll() {
        List<StudentDorm> studentDorms = dormitoryDao.selectAll();


        return studentDorms;
    }

    @Override
    public Page<StudentDorm> search(Map searchMap) {
        Example example = new Example(StudentDorm.class);
        int pageNum=1;
        int pageSize=2;
        if(searchMap!=null)
        {


            Example.Criteria criteria = example.createCriteria();
            if(searchMap.get("dormId") !="")
            {
                criteria.andEqualTo("did",searchMap.get("dormId"));
            }
           if((Integer) searchMap.get("floor") !=null)
           {
               
               if((Integer) searchMap.get("floor") !=0)
                   criteria.andBetween("did",(int)searchMap.get("floor")*100,(int)searchMap.get("floor")*100+100);
           }


            if((Integer) searchMap.get("pageNum") !=null){
                pageNum = (Integer) searchMap.get("pageNum");
            }

            if((Integer) searchMap.get("pageSize") !=null){
                pageSize = (Integer) searchMap.get("pageSize");
            }
        }
        PageHelper.startPage(pageNum,pageSize);  //pageHelper插件
        Page<StudentDorm> studentDorms = (Page<StudentDorm>) dormitoryDao.selectByExample(example);
        return studentDorms;


    }

    @Override
    public Boolean add(StudentDorm stuDorm) {
        UserInfo uinfo=new UserInfo();
        uinfo.setUid(Integer.toString(stuDorm.getDid()));
        uinfo.setPassWord(uinfo.getUid());
        uinfo.setType("0");
        int re1=adduser.insert(uinfo);
        int re2= dormitoryDao.insert(stuDorm);
        if(re2>0)
            return true;
        else
            return false;
    }

    @Override
    public StudentDorm findById(Integer id) {
        return dormitoryDao.selectByPrimaryKey(id);
    }

    @Override
    public Boolean count(Integer id) throws IOException {
        boolean count ;
        SelectId slid;
        slid=new SelectId("mybatis-configuration.xml");
        count =slid.selectId(id);
        return count;
    }

    @Override
    public Boolean update(StudentDorm dormitory) {
        int row = dormitoryDao.updateByPrimaryKeySelective(dormitory);
        if(row>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean updateStatus(String status, Integer id) {
        StudentDorm stuDorm = new StudentDorm();
        stuDorm.setDid(id);
        stuDorm.setElcStatus(status);
        int row = dormitoryDao.updateByPrimaryKeySelective(stuDorm);
        if(row>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean del(List<Integer> did) {
        for(Integer id:did) {
            dormitoryDao.deleteByPrimaryKey(id);
            adduser.deleteByPrimaryKey(Integer.toString(id));
        }
        return true;
    }

    //flag：
    //     1：用户名不存在    2：密码不对  3: 学生   4：管理员
    @Override
    public Integer login(String userId, String passWord) throws IOException {
        int flag ;
        SelectId login;
        login=new SelectId("mybatis-configuration.xml");
        flag =login.login(userId,passWord);
        return flag;
    }

    @Override
    public Integer CountNum() throws IOException {
        int num ;
        SelectId countNum;
        countNum=new SelectId("mybatis-configuration.xml");
        num =countNum.CountNum();
        return num;

    }
}
